<template>
  <div class="comment-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>评论管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <template>
        <el-table
          :data="comments"
          style="width: 100%"
          stripe
          class="table-list"
          size="small"
        >
          <el-table-column prop="title" label="标题"> </el-table-column>
          <el-table-column prop="total_comment_count" label="总评论数">
          </el-table-column>
          <el-table-column prop="fans_comment_count" label="粉丝评论数">
          </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">
              {{ scope.row.comment_status ? '正常' : '关闭' }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.comment_status"
                :disabled="scope.row.statusDisabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="onStatusChange(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size.sync="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticles, updateCommentStatus } from '@/api/article'
export default {
  name: 'Comment',
  components: {},
  props: {},
  data () {
    return {
      currentPage: 1,
      totalCount: 0, // 总页码条数
      pageSize: 10,
      comments: [] // 评论列表
    }
  },
  computed: {},
  watch: {},
  mounted () {
    this.loadArticles()
  },
  methods: {
    // 获取评论列表
    async loadArticles (currentPage = 1) {
      this.currentPage = currentPage
      const { data } = await getArticles({
        response_type: 'comment',
        page: currentPage,
        per_page: this.pageSize
      })
      // console.log(data)
      const { results } = data.data
      // 遍历评论列表是每条评论加上statusDisabled属性
      results.forEach(comment => {
        comment.statusDisabled = false
      })
      this.comments = results
      this.totalCount = data.data.total_count
    },
    // 操作改变评论状态
    async onStatusChange (comment) {
      // 禁用开关
      comment.statusDisabled = true
      // 发请求修改评论状态
      await updateCommentStatus(comment.id.toString(), comment.comment_status)
      comment.statusDisabled = false
      this.$message({
        type: 'success',
        message: comment.comment_status ? '开启文章评论状态成功' : '关闭文章评论状态成功'
      })
    },
    handleSizeChange (pageSize) {
      this.loadArticles(pageSize)
    },
    handleCurrentChange (currentPage) {
      // 页码改变，加载指定页码数据
      this.loadArticles(currentPage)
    }
  }
}
</script>

<style lang="less" scoped>
.table-list {
  margin-bottom: 15px;
}
</style>
